<template>
    <mescroll-uni
        ref="mescrollRef"
        @init="mescrollInit"
        :height="height"
        :down="downOption"
        @down="downRefresh"
        :up="upOption"
        @up="getData">
        <view class="cost-list">
            <cost-list-item
                :type="type"
                v-for="(item, index) in datas"
                :item="item"
                :index="index"
                :key="index"></cost-list-item>
        </view>
    </mescroll-uni>
</template>

<script>
import costListItem from "./cost-list-item.vue"
import { getSaleDetailPage } from "@/service/api/erp.js"
import { mapState } from "vuex"

export default {
    components: {
        costListItem,
    },
    props: {
        height: {
            type: String,
            default: "300px",
        },
        type: {
            type: String / Number,
            default: 2,
        },
    },
    computed: {
        ...mapState("report", ["startDate", "endDate", "selectedSalesman","selectedCustoms","selectedProject","selectedMarketing"]),
    },
    data() {
        return {
            datas: [],
            allLength: 0,
            param: {
                type: this.type,
                pageNumber: 0,
                pageSize: 0,
                sortType: 0, // 0：不排序；1：升序；2：降序
                sortColumn: "", // Total 净销售额  MaoriProfit 净利润
            },
            downOption: {
                use: true,
            },
            isLoaded: false,
        }
    },
    methods: {
        //获取列表数据
        getData(page) {
            let param = this.param
            param.pageNumber = page.num
            param.pageSize = page.size
            if (this.selectedSalesman) {
                param.eCodeArr = [this.selectedSalesman.UserCode]
            } else {
                delete param.eCodeArr
            }
			if (this.selectedCustoms.length >0) {
				param.bCodeArr = []
				this.selectedCustoms.forEach((e)=>{
					param.bCodeArr.push(e.UserCode)
				})
			}else{
				delete param.bCodeArr
			}
			if (this.selectedProject.length >0) {
				param.projectArr = []
				this.selectedProject.forEach((e)=>{
					param.projectArr.push(e.ProjectNo)
				})
			}else{
				delete param.projectArr
			}
			if (this.selectedMarketing.length >0) {
				param.organizationsArr = []
				this.selectedMarketing.forEach((e)=>{
					param.organizationsArr.push(e.OrganizationsNo)
				})
			}else{
				delete param.organizationsArr
			}
            getSaleDetailPage({
                ...param,
                startDate: this.startDate,
                endDate: this.endDate,
            }).then((res) => {
                if (res.State == 1) {
                    this.mescroll.endByPage(res.Data.length, res.TotalAmout)
                    this.datas = this.datas.concat(res.Data)
                    this.allLength = res.RecordAmout
                    this.isLoaded = true
                    this.$emit("totalBack", res.RecordAmout)
                } else {
                    this.mescroll.endBySize(0, 0)
                }
            })
        },
        //刷新
        refresh(param) {
            if (param) {
                this.param = param
            }
            this.downRefresh()
        },
        //刷新
        downRefresh() {
            this.datas = []
            this.mescroll.resetUpScroll()
        },
    },
    watch: {
        type(newVal) {
            this.param.type = newVal
        },
    },
}
</script>

<style lang="scss">
.cost-list {
    background: $color-white;
}
</style>
